import 'package:flutter_test/flutter_test.dart';
import 'package:awak/page/community/topic_data_holder.dart';
import 'package:awak/common/models/topic.dart';

void main() {
  group('Topic Persistence Tests', () {
    late List<Topic> testTopics;

    setUp(() {
      // 重置状态
      TopicDataHolder.allTopics.clear();
      TopicDataHolder.joinedTopicIds.clear();
      TopicDataHolder.topicPeopleCache.clear();
      TopicDataHolder.currentTopic = null;
      TopicDataHolder.currentTopicId = null;

      // 创建测试主题
      testTopics = [
        Topic(
          id: 1,
          title: 'Philosophical Thinking Topic Arrivals!',
          coverImage: 'topic1.jpg',
          content: '23',
          label: 'Philosophical',
          numberPeople: 98,
          userId: 1,
        ),
        Topic(
          id: 2,
          title: 'Travel Adventures Await!',
          coverImage: 'topic2.jpg',
          content: '15',
          label: 'traveling',
          numberPeople: 67,
          userId: 2,
        ),
        Topic(
          id: 3,
          title: 'Let\'s Think Carefully!',
          coverImage: 'topic3.jpg',
          content: '8',
          label: 'psychological learning',
          numberPeople: 42,
          userId: 3,
        ),
      ];

      TopicDataHolder.allTopics = testTopics;
    });

    test('should maintain selected topic after joining', () {
      // 模拟用户选择第二个主题
      TopicDataHolder.setCurrentTopic(testTopics[1]);
      expect(TopicDataHolder.currentTopicId, 2);
      expect(TopicDataHolder.currentTopic?.id, 2);

      // 模拟用户加入主题
      TopicDataHolder.joinTopic(2);
      TopicDataHolder.updateTopicPeopleCount(2, 68);

      // 验证状态保持
      expect(TopicDataHolder.isTopicJoined(2), true);
      expect(TopicDataHolder.getTopicPeopleCount(2), 68);
      expect(TopicDataHolder.currentTopicId, 2);
      expect(TopicDataHolder.currentTopic?.id, 2);
    });

    test('should not reset to first topic when user has selected another', () {
      // 用户选择第三个主题
      TopicDataHolder.setCurrentTopic(testTopics[2]);
      expect(TopicDataHolder.currentTopicId, 3);

      // 模拟页面重新加载主题数据（这是问题发生的场景）
      TopicDataHolder.allTopics = testTopics; // 重新设置主题列表

      // 确保状态一致性
      TopicDataHolder.ensureCurrentTopicConsistency();

      // 验证仍然是用户选择的主题，而不是第一个主题
      expect(TopicDataHolder.currentTopicId, 3);
      expect(TopicDataHolder.currentTopic?.id, 3);
      expect(TopicDataHolder.currentTopic?.title, 'Let\'s Think Carefully!');
    });

    test('should preserve join status and people count across topic switches', () {
      // 用户加入第一个主题
      TopicDataHolder.setCurrentTopic(testTopics[0]);
      TopicDataHolder.joinTopic(1);
      TopicDataHolder.updateTopicPeopleCount(1, 99);

      // 用户切换到第二个主题
      TopicDataHolder.setCurrentTopic(testTopics[1]);
      expect(TopicDataHolder.currentTopicId, 2);

      // 用户再切换回第一个主题
      TopicDataHolder.setCurrentTopic(testTopics[0]);

      // 验证之前的加入状态和人数仍然保持
      expect(TopicDataHolder.isTopicJoined(1), true);
      expect(TopicDataHolder.getTopicPeopleCount(1), 99);
      expect(TopicDataHolder.currentTopicId, 1);
    });

    test('should handle consistency when ID and object mismatch', () {
      // 设置不一致的状态（模拟可能的bug场景）
      TopicDataHolder.currentTopicId = 2;
      TopicDataHolder.currentTopic = testTopics[0]; // ID=1的主题

      // 调用一致性检查
      TopicDataHolder.ensureCurrentTopicConsistency();

      // 验证ID优先，对象被更新为正确的主题
      expect(TopicDataHolder.currentTopicId, 2);
      expect(TopicDataHolder.currentTopic?.id, 2);
      expect(TopicDataHolder.currentTopic?.title, 'Travel Adventures Await!');
    });

    test('should use setCurrentTopic to maintain consistency', () {
      // 使用新的setCurrentTopic方法
      TopicDataHolder.setCurrentTopic(testTopics[1]);

      // 验证ID和对象都正确设置
      expect(TopicDataHolder.currentTopicId, 2);
      expect(TopicDataHolder.currentTopic?.id, 2);
      expect(TopicDataHolder.currentTopic, testTopics[1]);

      // 设置为null
      TopicDataHolder.setCurrentTopic(null);
      expect(TopicDataHolder.currentTopicId, null);
      expect(TopicDataHolder.currentTopic, null);
    });

    test('should get updated topic data with cached people count', () {
      // 设置当前主题
      TopicDataHolder.setCurrentTopic(testTopics[0]);
      
      // 更新人数到缓存
      TopicDataHolder.updateTopicPeopleCount(1, 150);
      
      // 通过getTopicById获取主题应该返回缓存的人数
      final updatedTopic = TopicDataHolder.getTopicById(1);
      expect(updatedTopic?.numberPeople, 150);
      
      // 原始数据也应该被更新
      expect(TopicDataHolder.allTopics[0].numberPeople, 150);
    });
  });
}
